package com.jiang.daily.Q3542;

import java.util.Stack;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/11/10 10:44
 */
public class Solution {
    public int minOperations(int[] nums) {
        Stack<Integer> stack = new Stack<>();
        int res = 0;
        for (int n : nums) {
            while(!stack.isEmpty() && stack.peek() > n) {
                stack.pop();
            }
            if (n == 0) continue;
            while (stack.isEmpty() || stack.peek() < n) {
                stack.push(n);
                res++;
            }
        }
        return res;
    }

//    public int minOperations(int[] nums) {
//        int res = 0, top = -1;
//        for (int num : nums) {
//            while (top >= 0 && num < nums[top]) {
//                top--;
//                res++;
//            }
//            if (top < 0 || num != nums[top]) {
//                nums[++top] = num;
//            }
//        }
//        return res + top + (nums[0] > 0 ? 1 : 0);
//    }
}
